package com.xywy.fwc.adapter;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.xywy.R;
import com.xywy.common.util.ImageLoaderUtil;
import com.xywy.fwc.FwcWebView;
import com.xywy.fwc.bean.FwcAssea;
import com.xywy.fwc.bean.FwcItem;

public class FwcAsseaAdapter extends BaseAdapter {
	private List<List<FwcAssea>> list;
	private Context context;
	private Calendar calendar;
	private int width;
    private String name;
    DecimalFormat dcf ;
	
	public FwcAsseaAdapter(List<List<FwcAssea>> list, Context context,String name) {
		super();
		this.list = list;
		this.context = context;
		calendar = Calendar.getInstance();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		width = wm.getDefaultDisplay().getWidth() - dip2px(context, 50);
		this.name = name;
		dcf = new  DecimalFormat("00");
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(list == null)
			return 0;
		return list.size();
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	@Override
	public int getItemViewType(int position) {

		if (list.get(position).size() == 0) {
			return 0;
		} else {
			return 1;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final List<FwcAssea> items = list.get(position);
		int type = getItemViewType(position);
//		ViewHolderB bHolder = null;
		ViewHolderS sHolder = null;
				sHolder = new ViewHolderS();
				convertView = View.inflate(context, R.layout.fwc_assea_items,
						null);
				sHolder.bigImage = (ImageView) convertView
						.findViewById(R.id.bImage);
				sHolder.content = (TextView) convertView
						.findViewById(R.id.content);
				sHolder.time = (TextView) convertView.findViewById(R.id.time);
				sHolder.timelinear = (LinearLayout) convertView
						.findViewById(R.id.timelinear);
				sHolder.inerList = (ListView) convertView.findViewById(R.id.inerList);
				convertView.setTag(sHolder);
			setView(sHolder, position, items);
             sHolder.inerList.setAdapter(new InnerAdapter(items.subList(1, items.size()),context));
		
             sHolder.bigImage.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View view) {
					// TODO Auto-generated method stub
					FwcAssea fs = items.get(0);
					FwcItem fi = new FwcItem();
					fi.setTitle(name);
					fi.setUrl(fs.getUrl());
					Intent intent = new Intent(context, FwcWebView.class);
					intent.putExtra("item", fi);
					Stattistics(fs.getTitle(), fs.getUrl());
					context.startActivity(intent);
				}
			});
             return convertView;
	}

	static  class ViewHolderB {
		public TextView time;
		public TextView content;
		public ImageView bigImage;
		public LinearLayout timelinear;
	}

	static final class ViewHolderS extends ViewHolderB{
		public ListView inerList;
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
	
	public void setView(ViewHolderS sHolder,int position,List<FwcAssea> items){
		FwcAssea item = items.get(0);
		LayoutParams params;
		if(items.size()==1){
			LayoutParams paramsContent = new LayoutParams(width, dip2px(context, 50));
			paramsContent.topMargin = 30;
			paramsContent.addRule(RelativeLayout.CENTER_HORIZONTAL);
			sHolder.content.setLayoutParams(paramsContent);
			sHolder.content.setBackgroundColor(0xffffffff);
			sHolder.content.setText(item.getTitle());
			params = new LayoutParams(width, (5 * width) / 9);
			params.leftMargin = dip2px(context, 5);
			params.rightMargin = dip2px(context, 5);
			params.topMargin = dip2px(context, 5);
			params.bottomMargin = dip2px(context, 5);
			params.addRule(RelativeLayout.BELOW,R.id.content);
		}else {
			sHolder.content.setHeight(dip2px(context, 40));
			sHolder.content.setTextColor(0xffffffff);
			sHolder.content.setText(item.getTitle());
			params = new LayoutParams(width, (5 * width) / 9);
			params.leftMargin = dip2px(context, 5);
			params.rightMargin = dip2px(context, 5);
			params.topMargin = dip2px(context, 5);
			params.bottomMargin = dip2px(context, 5);
		}
		params.addRule(RelativeLayout.CENTER_HORIZONTAL);
		sHolder.bigImage.setLayoutParams(params);
		ImageLoaderUtil.displayImage(item.getImg_url(), sHolder.bigImage);
		sHolder.bigImage.setScaleType(ScaleType.FIT_XY);
		calendar.setTimeInMillis(Long.parseLong(item.getDatatime())*1000);
		sHolder.time.setText((calendar.get(Calendar.MONTH)+1) + "月 "+calendar.get(Calendar.DAY_OF_MONTH) + "日 "
				+ calendar.get(Calendar.HOUR_OF_DAY) + "："
				+ dcf.format(calendar.get(Calendar.MINUTE)));
		sHolder.timelinear.setVisibility(View.VISIBLE);
	}
	
	
	class InnerAdapter extends BaseAdapter{
         List<FwcAssea> list;
         Context context;
		public InnerAdapter(List<FwcAssea> list, Context context) {
			super();
			this.list = list;
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			convertView = View.inflate(context, R.layout.fwc_inerlist, null);
			final FwcAssea item = list.get(position);
			ImageView iv = (ImageView) convertView.findViewById(R.id.innerIv);
			TextView tv = (TextView) convertView.findViewById(R.id.contentInner);
			tv.setText(item.getTitle());
			ImageLoaderUtil.displayImage(item.getImg_url(), iv);
			convertView.setBackgroundResource(R.drawable.common_list_selector);
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					FwcItem fi = new FwcItem();
					fi.setTitle(name);
					fi.setUrl(item.getUrl());
					Intent intent = new Intent(context, FwcWebView.class);
					intent.putExtra("item", fi);
					Stattistics(fi.getTitle(), fi.getUrl());
					context.startActivity(intent);
				}
			});
			return convertView;
		}
		
	}
	
	
	/**
	 * 友盟统计收藏 帮助
	 */
	public void Stattistics(String title,String url){
		Map<String, String> params = new HashMap<String, String>();
			params.put("title",url); //代表来源
			MobclickAgent.onEvent(context, "Fwc_001", params);
	}
	

}
